package com.csc.service.impl;

import com.csc.entity.Student;
import com.csc.mapper.AllocationMapper;
import com.csc.mapper.StudentMapper;
import com.csc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private AllocationMapper allocationMapper;

    @Override
    public List<Student> findAll() {
        return studentMapper.findAll();
    }

    @Override
    public Student findById(Long id) {
        return studentMapper.findById(id);
    }

    @Override
    public void save(Student student) {
        if(student.getId()  == null) {
            studentMapper.insert(student);
        } else {
            studentMapper.update(student);
        }
    }

    @Override
    public void delete(Long id) {
        allocationMapper.cancelAllocation(id);
        studentMapper.delete(id);
    }
}